package articleInf

import (
	"bolg_admin/pkg/logger"
	"bolg_admin/pkg/model"
	"bolg_admin/pkg/utils"
)

func (inf *ArticleInf) CreateInf() (uint64, error) {
	if err := model.DB.Create(&inf).Error; err != nil {
		logger.LogError(err)
		return inf.ID, err
	}
	return inf.ID, nil
}

func SeArticleInf(page int, pageSize int) ([]ArticleInf, int64) {
	var articleInf []ArticleInf
	var count int64
	model.DB.Scopes(utils.Paginate(page, pageSize)).Find(&articleInf)
	model.DB.Model(&ArticleInf{}).Count(&count)
	return articleInf, count
}

type InfData struct {
	UserName  string     `json:"userName"`
	Title     string     `json:"title"`
	CreatedAt utils.Time `json:"created_at"`
}

func GetArticleInf(conId uint64) (InfData, error) {
	var inf InfData
	err := model.DB.Model(&ArticleInf{}).Where("articlecon_id = ?", conId).Find(&inf).Error
	if err != nil {
		logger.LogError(err)
		return inf, err
	}
	return inf, err
}
